//
//  CustomPickView.m
//  Project_Demo
//
//  Created by sisi on 2018/7/15.
//  Copyright © 2018年 caoxiaozhu. All rights reserved.
//

#import "CustomPickView.h"

@interface CustomPickView ()<UITextFieldDelegate>

@end

@implementation CustomPickView

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITextField *textField = [[UITextField alloc]init];
    textField.placeholder = @"请输入你的出生年月";
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.size = CGSizeMake(200, 30);
    [textField setOrigin:CGPointMake(100, 100)];
    textField.delegate = self;
    textField.tag = 1;
    [self.view addSubview:textField];
    
    UITextField *localField = [UITextField textFieldWithPlaceHolder:@"请输入住址" PlaceHolderColor:nil BorderType:UITextBorderStyleRoundedRect SecureTextEntry:false Text:nil TextColor:Color_Black BackgroundColor:nil KeyboardType:UIKeyboardTypeDefault];
    
    localField.tag = 2;
    localField.delegate = self;
    [localField setSize:CGSizeMake(200, 30)];
    [localField setOrigin:CGPointMake(100, 150)];
    [self.view addSubview:localField];
    
    UITextField *colorPickerField = [UITextField textFieldWithPlaceHolder:@"点击彩色选择框" PlaceHolderColor:nil BorderType:UITextBorderStyleRoundedRect SecureTextEntry:false Text:nil TextColor:Color_Black BackgroundColor:nil KeyboardType:UIKeyboardTypeDefault];
    
    colorPickerField.tag = 3;
    colorPickerField.delegate = self;
    [colorPickerField setSize:CGSizeMake(200, 30)];
    [colorPickerField setOrigin:CGPointMake(100, 200)];
    [self.view addSubview:colorPickerField];
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    [self handlerTextFieldSelect:textField];
    return NO;
}

- (void)handlerTextFieldSelect:(UITextField *)textField {
    switch (textField.tag) {
        case 1:
        {
            NSDate *minDate = [NSDate br_setYear:1990 month:3 day:12];
            NSDate *maxDate = [NSDate date];
            [BRDatePickerView showDatePickerWithTitle:@"出生日期" dateType:BRDatePickerModeYMD defaultSelValue:textField.text minDate:minDate maxDate:maxDate isAutoSelect:YES themeColor:nil resultBlock:^(NSString *selectValue) {
                textField.text = selectValue;
            } cancelBlock:^{
                NSLog(@"点击了背景或取消按钮");
            }];
        }
            break;
            
        case 2:{
            // 【转换】：以@" "自字符串为基准将字符串分离成数组，如：@"浙江省 杭州市 西湖区" ——》@[@"浙江省", @"杭州市", @"西湖区"]
            NSArray *defaultSelArr = [textField.text componentsSeparatedByString:@" "];
            // NSArray *dataSource = [weakSelf getAddressDataSource];  //从外部传入地区数据源
            NSArray *dataSource = nil; // dataSource 为空时，就默认使用框架内部提供的数据源（即 BRCity.plist）
            [BRAddressPickerView showAddressPickerWithShowType:BRAddressPickerModeArea dataSource:dataSource defaultSelected:defaultSelArr isAutoSelect:YES themeColor:nil resultBlock:^(BRProvinceModel *province, BRCityModel *city, BRAreaModel *area) {
                textField.text = [NSString stringWithFormat:@"%@ %@ %@", province.name, city.name, area.name];
                NSLog(@"省[%@]：%@，%@", @(province.index), province.code, province.name);
                NSLog(@"市[%@]：%@，%@", @(city.index), city.code, city.name);
                NSLog(@"区[%@]：%@，%@", @(area.index), area.code, area.name);
                NSLog(@"--------------------");
            } cancelBlock:^{
                NSLog(@"点击了背景视图或取消按钮");
            }];
        }
            break;
        default:
        {
            NSString *dataSource = @"testData1.plist"; // 可以将数据源（上面的数组）放到plist文件中
            [BRStringPickerView showStringPickerWithTitle:@"学历" dataSource:dataSource defaultSelValue:textField.text isAutoSelect:YES themeColor:[UIColor redColor] resultBlock:^(id selectValue) {
                textField.text  = selectValue;
            } cancelBlock:^{
                NSLog(@"点击了背景视图或取消按钮");
            }];
        }
            break;
    }
}
@end
